package com.ruoyi.system.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.exception.GlobalException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.system.api.RemoteClassaiService;
import com.ruoyi.system.api.domain.SysOperApiLog;
import com.ruoyi.system.api.domain.vo.TeacherLogVo;
import com.ruoyi.system.service.ISysOperApiLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

;

/**
 * 自定义操作日志记录Controller
 * 
 * @author ruoyi
 * @date 2025-05-16
 */
@Tag(name = "自定义日志", description = "自定义操作日志接口")
@RestController
@RequestMapping("/apiLog")
public class SysOperApiLogController extends BaseController
{
    @Autowired
    private ISysOperApiLogService sysOperApiLogService;
    @Autowired
    private RemoteClassaiService classaiService;

    /**
     * 添加日志
     * @param operLog 日志信息
     * @return AjaxResult
     */
    @InnerAuth
    @PostMapping("/operlog")
    public AjaxResult add(@RequestBody SysOperApiLog operLog)
    {
        operLog.setOperTime(DateUtil.date());
        sysOperApiLogService.save(operLog);
        return AjaxResult.success("操作成功");
    }


    /**
     *  查询日志列表
     * @param schoolId 学校id
     * @param title 交易类型名称
     * @param operatorType 操作类型
     * @param businessType 渠道
     * @param operName 操作人员
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param userId 用户id
     * @return 日志列表
     */
    @Operation(summary = "查询日志列表")
    @GetMapping("/list")
    public TableDataInfo list(@RequestHeader("Client-Type")String clientType,String schoolId,String title,Integer operatorType,Integer businessType,
                              String operName,String startTime,String endTime,String userId) {

        startPage();
        List<SysOperApiLog> list;
        if (Objects.equals(clientType,"01")){
            if (StringUtils.isEmpty(userId)){
                throw new GlobalException("用户ID不能为空");
            }
            list = sysOperApiLogService.list(schoolId,title,operatorType,businessType,operName,startTime,endTime,userId);
        }else {
            list = sysOperApiLogService.list(schoolId,title,operatorType,businessType,operName,startTime,endTime,null);
        }
        if (CollectionUtil.isNotEmpty(list)){
            list.forEach(d->{
                if (StringUtils.isNotEmpty(d.getSchoolId())){
                    AjaxResult ajaxResult=classaiService.getSchoolInfo(d.getSchoolId(), SecurityConstants.INNER);
                    if (ajaxResult.isSuccess()){
                        String data= JSON.toJSONString(ajaxResult.getData());
                        if (StringUtils.isNotEmpty(data)) {
                            JSONObject jsonObject = JSON.parseObject(data);
                            d.setSchoolName(jsonObject.getString("name"));
                        }
                    }
                }
            });
        }
        return getDataTable(list);
    }


    /**
     *  导出日志
     * @param schoolId 学校id
     * @param title 交易类型名称
     * @param operatorType 操作类型
     * @param businessType 渠道
     * @param operName 操作人员
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param userId 用户id
     */
    @Operation(summary = "导出日志")
    @PostMapping("/export")
    public void export(@RequestHeader("Client-Type")String clientType,HttpServletResponse response, String schoolId,String title,Integer operatorType,Integer businessType,
                       String operName,String startTime,String endTime,String userId) {

        List<SysOperApiLog> sysOperApiLogList;
        if (Objects.equals(clientType,"01")) {
            sysOperApiLogList = sysOperApiLogService.list(schoolId, title, operatorType, businessType, operName, startTime, endTime, userId);
            List<TeacherLogVo> collect = sysOperApiLogList.stream().map(list -> {
                TeacherLogVo vo = new TeacherLogVo();
                BeanUtils.copyProperties(list,vo);
                //省略其他操作
                return vo;
            }).collect(Collectors.toList());
            ExcelUtil<TeacherLogVo> util = new ExcelUtil<>(TeacherLogVo.class);
            util.exportExcel(response, collect, "老师操作日志");
        }else {
            sysOperApiLogList = sysOperApiLogService.list(schoolId,title,operatorType,businessType,operName,startTime,endTime,null);
            ExcelUtil<SysOperApiLog> util = new ExcelUtil<>(SysOperApiLog.class);
            util.exportExcel(response, sysOperApiLogList, "操作日志");
        }
    }

}
